<template>
    <div class="content">
        <div class="common-seach-wrap" :style="{ marginBottom: '10px' }">
            <el-button type="primary" icon="Plus" @click="handleOpen" v-auth="'examine_add'">添加模板</el-button>
        </div>
        <el-table :data="tableData" row-key="id" height="100%" stripe>
            <el-table-column type="index" label="#" fixed="left" />
            <el-table-column prop="title" label="标题" fixed="left" />
            <el-table-column prop="create_time" sortable label="创作时间" />
            <el-table-column prop="update_time" sortable label="最后修改" />
            <el-table-column label="操作" fixed="right" width="200">
                <template #default="scope">
                    <el-button type="primary" link icon="Edit" @click="handleOpen(scope.row.id)"
                        v-auth="'examine_edit'">
                        编辑
                    </el-button>
                    <el-button type="danger" link icon="DeleteFilled" @click="delInfo(scope.row.id)"
                        v-auth="'examine_del'">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-divider border-style="none" />
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background
            :current-page="curPage" :page-size="pageSize" layout="total, sizes,prev, pager, next, jumper"
            :total="total"></el-pagination>
        <Drawer ref="examineDraw" @getTable="getTableInfo" :department="departmentOption" />
    </div>
</template>

<script setup lang="ts">
import { ref, onMounted, defineEmits, reactive, watch } from "vue";
import {
    deleteDepartment,
    getDepartmentOptionsApi,
} from "@/api/company/department";
import Drawer from "./component/dialogForm.vue";
import { ElMessage, ElMessageBox, TabPaneName } from "element-plus";
import { getTplListAPi } from "@/api/contract/contract";
const activeName = ref("0");
const tabs = ref<any[]>([]);
const classVisible = ref(false);
const classDialogTitle = ref("添加分类");
const examineDraw = ref<any>(null);
const classForm = ref({
    id: 0,
    title: "",
    sort: 0,
});
const query = reactive({
    department_id: [],
    class_id: "0",
});
const total = ref(0);
watch(
    query,
    () => {
        getTableInfo();
    },
    { deep: true }
);
watch(activeName, (newVal) => {
    query.class_id = newVal;
});
const curPage = ref(1);
const pageSize = ref(20);
const departmentOption = ref<any[]>([]);
const childDialogForm = ref<any>(null);
const tableData = ref([]);
const staffRef = ref<any>(null);
onMounted(() => {
    getTableInfo();
});
const handleSizeChange = (val: number) => {
    pageSize.value = val;
    getTableInfo();
};
const handleCurrentChange = (val: number) => {
    curPage.value = val;
    getTableInfo();
};
function getTableInfo() {
    getTplListAPi({
        page: curPage.value,
        page_size: pageSize.value,
        ...query,
    }).then((res) => {
        tableData.value = res.data.data;
        total.value = res.data.total;
    });
}

function delInfo(id: number) {
    ElMessageBox.confirm("确认要删除此信息吗?", "警告！", {
        confirmButtonText: "确认删除",
        cancelButtonText: "取消",
        type: "warning",
    })
        .then(() => {
            deleteDepartment(id).then((res: any) => {
                getTableInfo();

                ElMessage({
                    type: "success",
                    message: "删除成功！",
                });
            });
        })
        .catch(() => {
            ElMessage({
                type: "info",
                message: "删除已取消",
            });
        });
}

// 打开添加数据窗口
const emits = defineEmits(["openForm", "search"]);
function handleOpen(id?: number) {
    examineDraw.value.dialogOpen(id || 0);
}

const handleStaff = (e: any, checked: boolean) => {
    if (checked) {
        query.department_id.push(e.id);
    } else {
        query.department_id = query.department_id.filter(
            (item: any) => item != e.id
        );
    }
};

defineExpose({
    getTableInfo,
});
</script>

<style lang="scss">
.el-tabs {
    .el-tabs__new-tab {
        width: auto !important;
    }
}

.table-box {
    height: calc(100% - 200px);
}
</style>